package com.java.main.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.java.main.constant.Errors;
import com.java.main.controller.output.BaseOutput;
import com.java.main.domain.IdEntity.IdEntityAuto;
import com.java.main.exception.HiBabyGlobalException;
import com.java.main.service.BaseService;
import com.java.main.util.JsonUtil;
import com.java.main.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.java.main.controller.output.ResultResponseBeanUtil;
//import com.hibaby.crm.domain.role.RoleDO;

import io.swagger.annotations.ApiOperation;

//@Api(value = "通用接口", description = "通用接口，无法通过swagger-ui调试，请用postman", produces = "application/json")
@Controller
public class GeneralController extends BaseController {
	@Autowired
	private BaseService<IdEntityAuto, Integer> baseService;
	private static Map<String, String> tableNameToClass = null;
	static {
		tableNameToClass = new HashMap<String, String>();
		tableNameToClass.put("role", "com.hibaby.crm.domain.role.RoleDO");
	}

	@ApiOperation(value = "通用添加接口", notes = "通用添加接口")
	@ResponseBody
	@RequestMapping(value = "/general/{tableName}/add", method = RequestMethod.POST)
	public BaseOutput<Void> add(HttpServletRequest request, @PathVariable("tableName") String tableName)
			throws Exception {
		String json = StringUtils.requestToJson(request);
		String className = tableNameToClass.get(tableName);
		if (className == null) {
			throw new HiBabyGlobalException(Errors.TRANSFER_ENCODING_NOT_EXIST);
		}
		IdEntityAuto entity = (IdEntityAuto) JsonUtil.jsonToBean(json, Class.forName(className));
		entity.setKbmContent(getCurrentInfo(request));
		baseService.add(entity);
		return ResultResponseBeanUtil.success();
	}

//	@ApiOperation(value = "角色表", notes = "可操作角色表")
//	@ResponseBody
//	@RequestMapping(value = "/general/role", method = RequestMethod.GET)
//	public BaseOutput<Void> role(@RequestBody RoleDO input) throws Exception {
//		return ResultResponseBeanUtil.success();
//	}
}
